package gui;

import java.awt.Image;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import gui.exception.ViewNotFoundException;
import gui.framework.WindowTools;
import gui.framework.intent.Bundle;
import gui.framework.intent.Intent;
import gui.framework.intent.IntentFilter;
import gui.parser.LayoutParser;
import gui.view.View;
import gui.view.ViewGroup;

public class Activity extends Application {
	private JFrame window;
	private IntentFilter intentFilter;
	private View root;
	private String title;
	private boolean visible;
	private boolean debug;
	private Intent intent;

	public IntentFilter getIntentFilter() {
		return intentFilter;
	}

	public void setIntentFilter(IntentFilter intentFilter) {
		this.intentFilter = intentFilter;
	}

	public void onCreate(Bundle savedInstanceState) {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onCreate");
	}

	protected void onForeground() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onForeground");
	}

	protected void onOpened() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onOpened");
	}

	protected void onPause() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onPause");
	}

	protected void onBackground() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onBackground");
	}

	protected void onDestory() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onDestory");
	}

	protected void onRestart() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":onRestart");
	}

	/**
	 * 最小化生命周期方法
	 * 
	 */
	public void iconified() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":iconified");
	}

	/**
	 * 从最小化状态恢复
	 * 
	 */
	public void deiconified() {
		if (isDebug())
			System.out.println(this.getClass().getName() + ":deiconified");
	}

	protected void onFinish() {
		window.dispose();
	}

	public void setContentView(int layoutId) {
		if (root == null)
			root = new LayoutParser(layoutId).getLayout();
		if (root instanceof ViewGroup)
			((ViewGroup) root).invilidate(true);
		else
			root.invalidate();
		window = new JFrame();
		window.add(root.getView());
		int width = root.getWidth(), height = root.getHeight();
		if (width == -1 && height != -1) {
			window.setExtendedState(JFrame.MAXIMIZED_HORIZ);
		} else if (width != -1 && height == -1) {
			window.setExtendedState(JFrame.MAXIMIZED_VERT);
		} else if (width == -1 && height == -1) {
			window.setExtendedState(JFrame.MAXIMIZED_BOTH);
		}
		window.setSize(width, height);
		if (window != null) {
			if (root.layout_gravity.toLowerCase().equals(CENTER)) {
				WindowTools.locateFrameCenter(window);
			}
		}
		window.setVisible(true);
		window.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				onOpened();
			}

			@Override
			public void windowIconified(WindowEvent e) {
				iconified();
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				deiconified();
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				onBackground();
			}

			@Override
			public void windowClosing(WindowEvent e) {
				onPause();
				onFinish();
			}

			@Override
			public void windowClosed(WindowEvent e) {
				onDestory();
				visible = false;
			}

			@Override
			public void windowActivated(WindowEvent e) {
				visible = true;
				onForeground();
			}
		});
		window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}

	public void setTitle(String title) {
		this.title = title;
		window.setTitle(title);
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public Intent getIntent() {
		return intent;
	}

	public String getTitle() {
		return title;
	}

	public boolean isVisible() {
		return visible;
	}

	public boolean isDebug() {
		return debug;
	}

	@Override
	public String getLabel() {
		return super.getLabel();
	}

	@Override
	public void setLabel(String label) {
		super.setLabel(label);
	}

	public View getRoot() {
		return root;
	}

	public void setIcon(int icon) {
		window.setIconImage(getResource().getImage(icon).getImage());
	}

	public Image getIcon() {
		return window.getIconImage();
	}

	public void setBackground(int resid) {
		root.setBacground(resid);
	}

	public ImageIcon getBacground() {
		return root.getBacground();
	}

	public View findViewById(int id) {
		View v = null;
		if (root instanceof ViewGroup) {
			v = ((ViewGroup) root).findViewById(id);
		}
		if (v != null)
			return v;
		throw new ViewNotFoundException();
	}
}
